import { UserModel } from "../account_server/model/UserModel";
import { ErrorCode } from "../src/config/ErrorCode";
import { SocketConnection } from "../src/engine/socketserver/socket/SocketConnection";
import { SocketConnectionManager } from "../src/engine/socketserver/socket/SocketConnectionManager";
import { SocketMsg } from "../src/engine/socketserver/socket/SocketMsg";
import DateUtil from "../src/engine/utils/DateUtil";
import { Logger } from "../src/engine/utils/Logger";
import { JhaoProto } from "../src/lib/net/protobuf/jhaoproto";
import { ProtoManager } from "../src/lib/net/protobuf/ProtoManager";
import { ResponseModel } from "../src/model/ResponseModel";
import { ClubConfig } from "./config/ClubConfig";
import { ClubDao } from "./dao/ClubDao";
import { ClubUserDao } from "./dao/ClubUserDao";
import { RoomTemplateDao } from "./dao/RoomTemplateDao";
import { ClubEntity } from "./entity/ClubEntity";
import { ClubUserEntity } from "./entity/ClubUserEntity";
import { ClubModel } from "./model/ClubModel";
import { ServerManager } from '../src/server/ServerManager'
import { RoomTemplateModel } from "./model/RoomTemplateModel";

export class Club_SocketMsgHandler {

    public static onEventSocketGata(socketConnect: SocketConnection, msg: JhaoProto.BaseMsg) {
        if (msg.cmdIndex == JhaoProto.CmdIndex.club_list) {
            this.club_list(socketConnect, msg)
        }
        else if (msg.cmdIndex == JhaoProto.CmdIndex.club_create) {
            this.club_create(socketConnect, msg)
        }
        else if (msg.cmdIndex == JhaoProto.CmdIndex.club_join) {
            this.club_join(socketConnect, msg)
        }
        else if (msg.cmdIndex == JhaoProto.CmdIndex.club_apply_list) {
            this.club_apply_list(socketConnect, msg)
        }
        else if (msg.cmdIndex == JhaoProto.CmdIndex.club_apply_handle) {
            this.club_apply_handle(socketConnect, msg)
        }
        else if (msg.cmdIndex == JhaoProto.CmdIndex.club_exit) {
            this.club_exit(socketConnect, msg)
        }
        else if (msg.cmdIndex == JhaoProto.CmdIndex.club_member_list) {
            this.club_member_list(socketConnect, msg)
        }
        else if (msg.cmdIndex == JhaoProto.CmdIndex.club_delete_member) {
            this.club_delete_member(socketConnect, msg)
        }
        else if (msg.cmdIndex == JhaoProto.CmdIndex.club_member_actor) {
            this.club_member_actor(socketConnect, msg)
        }
        else if (msg.cmdIndex == JhaoProto.CmdIndex.club_score) {
            this.club_score(socketConnect, msg)
        }
        else if (msg.cmdIndex == JhaoProto.CmdIndex.club_create_room_template) {
            this.club_create_room_template(socketConnect, msg)
        }
        else if (msg.cmdIndex == JhaoProto.CmdIndex.club_delete_room_template) {
            this.club_delete_room_template(socketConnect, msg)
        }
        else if (msg.cmdIndex == JhaoProto.CmdIndex.club_room) {
            this.club_room(socketConnect, msg)
        }
    }

    private static async club_list(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let resultClubs: Array<JhaoProto.IClubInfo> = await ClubDao.get_club_list(conn.user.userid)
        let respData: JhaoProto.Iresp_club_list = {
            clubs: resultClubs
        }
        conn.createMsgToClient(socketMsg.cmdIndex, socketMsg.cmdOrder, JhaoProto.resp_club_create, respData)
    }

    private static async club_create(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_club_create = ProtoManager.instance.decode(JhaoProto.req_club_create, socketMsg.data)
        let clubid: number = await ClubDao.randomClubId();
        let respModel: ResponseModel = new ResponseModel()
        if (clubid > 0) {
            let creator: number = conn.user.userid;
            let clubEntity: ClubEntity = new ClubEntity()
            clubEntity.club_name = protoPack.name
            clubEntity.club_state = ClubConfig.Club_State_Type.Normal
            clubEntity.clubid = clubid
            clubEntity.create_time = DateUtil.formatDateStr(DateUtil.now())
            clubEntity.creator = creator
            clubEntity.notice = protoPack.notice
            let isSuc: boolean = await ClubDao.create_club(clubEntity)
            if (isSuc) {
                respModel.code = ErrorCode.Code.Suc;
            } else {
                respModel.code = ErrorCode.Code.Fail;
                respModel.msg = "创建俱乐部数据库失败，稍后再试"
            }
        } else {
            respModel.code = ErrorCode.Code.Fail
            respModel.msg = "随机id失败，稍后再试"
        }
        if (respModel.code == ErrorCode.Code.Suc) {
            this.club_list(conn, socketMsg)
        } else {
            conn.sendErrorToClient(socketMsg.cmdOrder, respModel.code, respModel.msg)
        }
    }

    private static async club_join(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_club_join = ProtoManager.instance.decode(JhaoProto.req_club_join, socketMsg.data)
        let respModel: ResponseModel = await ClubDao.join_club(conn.user.userid, protoPack.clubid)
        if (respModel.code == ErrorCode.Code.Suc) {
            let respData: JhaoProto.Iresp_club_join = {
            }
            conn.createMsgToClient(socketMsg.cmdIndex, socketMsg.cmdOrder, JhaoProto.resp_club_join, respData)
        } else {
            conn.sendErrorToClient(socketMsg.cmdOrder, respModel.code, respModel.msg)
        }
    }

    private static async club_apply_list(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_club_apply_list = ProtoManager.instance.decode(JhaoProto.req_club_apply_list, socketMsg.data)
        let users: Array<JhaoProto.IClub_User> = await ClubUserDao.req_club_apply_list(protoPack.clubid)
        let respData: JhaoProto.Iresp_club_apply_list = {
            users: users
        }
        conn.createMsgToClient(socketMsg.cmdIndex, socketMsg.cmdOrder, JhaoProto.resp_club_apply_list, respData)
    }

    private static async club_apply_handle(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_club_apply_handle = ProtoManager.instance.decode(JhaoProto.req_club_apply_handle, socketMsg.data)
        await ClubUserDao.club_apply_handle(protoPack.id, protoPack.isAgree, conn.user.userid)
        let respData: JhaoProto.Iresp_club_apply_handle = {
        }
        conn.createMsgToClient(socketMsg.cmdIndex, socketMsg.cmdOrder, JhaoProto.resp_club_apply_handle, respData)
        if (protoPack.isAgree) {
            let notifyConn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(protoPack.userid)
            if (notifyConn) {
                let resultClubs: Array<JhaoProto.IClubInfo> = await ClubDao.get_club_list(protoPack.userid)
                let respData: JhaoProto.Iresp_club_list = {
                    clubs: resultClubs
                }
                notifyConn.createMsgToClient(JhaoProto.CmdIndex.club_list, 0, JhaoProto.resp_club_list, respData)
            }
        }
    }

    private static async club_exit(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_club_exit = ProtoManager.instance.decode(JhaoProto.req_club_exit, socketMsg.data)
        let clubUser: ClubUserEntity = await ClubUserDao.get_club_user(protoPack.clubid, conn.user.userid)
        if (clubUser) {
            clubUser.user_state = ClubConfig.Club_merber_state_type.refuse
            clubUser = await ClubUserDao.update_user(clubUser)
            let respData: JhaoProto.Iresp_club_exit = {
            }
            conn.createMsgToClient(JhaoProto.CmdIndex.club_exit, socketMsg.cmdOrder, JhaoProto.resp_club_exit, respData)
        }
        else {
            conn.sendErrorToClient(socketMsg.cmdOrder, JhaoProto.ErrorCodeWs.Fail, "不在亲友圈")
        }
    }

    private static async club_member_list(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_club_member_list = ProtoManager.instance.decode(JhaoProto.req_club_member_list, socketMsg.data)
        let users: Array<JhaoProto.IClub_User> = await ClubUserDao.req_club_member_list(protoPack.clubid)
        let respData: JhaoProto.Iresp_club_member_list = {
            users: users
        }
        conn.createMsgToClient(socketMsg.cmdIndex, socketMsg.cmdOrder, JhaoProto.resp_club_member_list, respData)
    }

    private static async club_delete_member(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_club_delete_member = ProtoManager.instance.decode(JhaoProto.req_club_delete_member, socketMsg.data)
        let myClubUser: ClubUserEntity = await ClubUserDao.get_club_user(protoPack.clubid, conn.user.userid)
        if (myClubUser) {
            if (myClubUser.actor == ClubConfig.Club_Actor_Type.owner || myClubUser.actor == ClubConfig.Club_Actor_Type.administrator) {
                let clubUser: ClubUserEntity = await ClubUserDao.get_club_user(protoPack.clubid, protoPack.userid)
                if (clubUser) {
                    if (clubUser.actor != ClubConfig.Club_Actor_Type.owner) {
                        clubUser.user_state = ClubConfig.Club_merber_state_type.refuse
                        clubUser = await ClubUserDao.update_user(clubUser)
                        let respData: JhaoProto.Iresp_club_delete_member = {
                        }
                        conn.createMsgToClient(JhaoProto.CmdIndex.club_delete_member, socketMsg.cmdOrder, JhaoProto.resp_club_delete_member, respData)
                    } else {
                        conn.sendErrorToClient(socketMsg.cmdOrder, JhaoProto.ErrorCodeWs.Fail, "圈主不能踢出")
                    }
                }
                else {
                    conn.sendErrorToClient(socketMsg.cmdOrder, JhaoProto.ErrorCodeWs.Fail, "不在亲友圈")
                }
            } else {
                conn.sendErrorToClient(socketMsg.cmdOrder, JhaoProto.ErrorCodeWs.Fail, "您不是管理员")
            }
        } else {
            conn.sendErrorToClient(socketMsg.cmdOrder, JhaoProto.ErrorCodeWs.Fail, "不在亲友圈")
        }
    }


    private static async club_member_actor(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_club_member_actor = ProtoManager.instance.decode(JhaoProto.req_club_member_actor, socketMsg.data)
        let myClubUser: ClubUserEntity = await ClubUserDao.get_club_user(protoPack.clubid, conn.user.userid)
        if (myClubUser) {
            if (myClubUser.actor == ClubConfig.Club_Actor_Type.owner || myClubUser.actor == ClubConfig.Club_Actor_Type.administrator) {
                let clubUser: ClubUserEntity = await ClubUserDao.get_club_user(protoPack.clubid, protoPack.userid)
                if (clubUser) {
                    if (clubUser.actor != ClubConfig.Club_Actor_Type.owner) {
                        if (protoPack.actor != ClubConfig.Club_Actor_Type.owner) {
                            clubUser.actor = protoPack.actor
                            clubUser = await ClubUserDao.update_user(clubUser)
                            let respData: JhaoProto.Iresp_club_member_actor = {
                            }
                            conn.createMsgToClient(JhaoProto.CmdIndex.club_member_actor, socketMsg.cmdOrder, JhaoProto.resp_club_member_actor, respData)
                        } else {
                            conn.sendErrorToClient(0, JhaoProto.ErrorCodeWs.Fail, "不能设置为圈主")
                        }
                    } else {
                        conn.sendErrorToClient(0, JhaoProto.ErrorCodeWs.Fail, "圈主不用设置管理员")
                    }
                }
                else {
                    conn.sendErrorToClient(0, JhaoProto.ErrorCodeWs.Fail, "不在亲友圈")
                }
            } else {
                conn.sendErrorToClient(0, JhaoProto.ErrorCodeWs.Fail, "您不是管理员")
            }
        } else {
            conn.sendErrorToClient(0, JhaoProto.ErrorCodeWs.Fail, "不在亲友圈")
        }
    }

    private static async club_score(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_club_score = ProtoManager.instance.decode(JhaoProto.req_club_score, socketMsg.data)
        let myClubUser: ClubUserEntity = await ClubUserDao.get_club_user(protoPack.clubid, conn.user.userid)
        if (myClubUser) {
            if (myClubUser.actor == ClubConfig.Club_Actor_Type.owner || myClubUser.actor == ClubConfig.Club_Actor_Type.administrator) {
                let clubUser: ClubUserEntity = await ClubUserDao.get_club_user(protoPack.clubid, protoPack.userid)
                if (clubUser) {
                    clubUser.score = clubUser.score + protoPack.score
                    clubUser = await ClubUserDao.update_user(clubUser)
                    let respData: JhaoProto.Iresp_club_score = {
                    }
                    conn.createMsgToClient(JhaoProto.CmdIndex.club_score, socketMsg.cmdOrder, JhaoProto.resp_club_score, respData)

                }
                else {
                    conn.sendErrorToClient(socketMsg.cmdOrder, JhaoProto.ErrorCodeWs.Fail, "不在亲友圈")
                }
            } else {
                conn.sendErrorToClient(socketMsg.cmdOrder, JhaoProto.ErrorCodeWs.Fail, "您不是管理员")
            }
        } else {
            conn.sendErrorToClient(socketMsg.cmdOrder, JhaoProto.ErrorCodeWs.Fail, "不在亲友圈")
        }
    }

    private static async club_create_room_template(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_club_create_room_template = ProtoManager.instance.decode(JhaoProto.req_club_create_room_template, socketMsg.data)
        let user: UserModel = conn.user
        let club: JhaoProto.IClubInfo = await ClubDao.get_club_simple(user.userid, protoPack.clubid)
        if (club) {
            let hadQuanXian: boolean = false
            if (club.actor == ClubConfig.Club_Actor_Type.owner) {
                hadQuanXian = true
            }
            else if (club.actor == ClubConfig.Club_Actor_Type.administrator) {
                hadQuanXian = true
            }
            if (hadQuanXian) {
                let dbSuc: boolean = await RoomTemplateDao.insert_room_template(user.userid, protoPack)
                if (dbSuc) {
                    ServerManager.instance.insertRoomTemplateModel()
                    let respData: JhaoProto.Iresp_club_create_room_template = {
                    }
                    conn.createMsgToClient(JhaoProto.CmdIndex.club_create_room_template, socketMsg.cmdOrder, JhaoProto.resp_club_create_room_template, respData)

                } else {
                    conn.sendErrorToClient(socketMsg.cmdOrder, JhaoProto.ErrorCodeWs.Fail, "插入房间模板时出错")
                }

            } else {
                conn.sendErrorToClient(socketMsg.cmdOrder, JhaoProto.ErrorCodeWs.Fail, "您没有此权限")
            }

        } else {
            conn.sendErrorToClient(socketMsg.cmdOrder, JhaoProto.ErrorCodeWs.Fail, "不在亲友圈")
        }
    }

    private static async club_delete_room_template(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_club_delete_room_template = ProtoManager.instance.decode(JhaoProto.req_club_delete_room_template, socketMsg.data)
        let user: UserModel = conn.user;
        let club: JhaoProto.IClubInfo = await ClubDao.get_club_simple(user.userid, protoPack.clubid)
        if(club){
            let hadQuanXian: boolean = false
            if (club.actor == ClubConfig.Club_Actor_Type.owner) {
                hadQuanXian = true
            }
            else if (club.actor == ClubConfig.Club_Actor_Type.administrator) {
                hadQuanXian = true
            }
            if (hadQuanXian) {
                let dbSuc: boolean = await RoomTemplateDao.delete_room_template(protoPack.id)
                if (dbSuc) {
                    ServerManager.instance.deleteRoomTemplate(protoPack.id)
                    conn.createMsgToClient(JhaoProto.CmdIndex.club_delete_room_template, socketMsg.cmdOrder)
                } else {
                    conn.sendErrorToClient(socketMsg.cmdOrder, JhaoProto.ErrorCodeWs.Fail, "房间模板已经不存在")
                }
            }
        }else{
            conn.sendErrorToClient(socketMsg.cmdOrder, JhaoProto.ErrorCodeWs.Fail, "不在亲友圈")
        }
    }


    private static async club_room(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_club_room = ProtoManager.instance.decode(JhaoProto.req_club_room, socketMsg.data)
        let club: JhaoProto.IClubInfo = await ClubDao.get_club_simple(conn.user.userid, protoPack.clubid);
        if (club) {
            let respData: JhaoProto.Iresp_club_room = {
                template: RoomTemplateModel.getRoomTemplateListByClub(protoPack.clubid),
            }
            conn.createMsgToClient(JhaoProto.CmdIndex.club_room, socketMsg.cmdOrder, JhaoProto.resp_club_room, respData)
        }
        else {
            // conn.sendErrorToClient(socketMsg.cmdOrder, JhaoProto.ErrorCodeWs.Fail, "不在亲友圈", JhaoProto.CmdIndex.club_room)
            conn.sendErrorToClient(socketMsg.cmdOrder, JhaoProto.ErrorCodeWs.Fail, "不在亲友圈")
        }
    }








}